package com.bsoft.webservice.impl;

import com.bsoft.Exception.CommonException;
import com.bsoft.service.CcisService;
import com.bsoft.service.HosptalOrderBillService;
import com.bsoft.service.RegisterService;
import com.bsoft.utils.JSONUtil;
import com.bsoft.utils.XmlMapUtils;
import com.bsoft.webservice.EntranceWebService;
import javafx.application.HostServices;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

@WebService(serviceName = "EntranceWebService",//对外发布的服务名
        targetNamespace = "http://service.bsoft.com",//指定你想要的名称空间，通常使用使用包名反转
        endpointInterface = "com.bsoft.webservice.EntranceWebService")
//服务接口全路径, 指定做SEI（Service EndPoint Interface）服务端点接口
@Component
public class EntranceWebServiceImpl implements EntranceWebService {
    private static Logger logger = LoggerFactory.getLogger(EntranceWebServiceImpl.class);


    @Autowired
    CcisService ccisService;

    @Autowired
    HosptalOrderBillService hosOrderBillService;


    public Object expHandler(Exception e) throws Exception {
        Map<String, Object> errMap = new HashMap<>();
        errMap.put("status", 500);
        errMap.put("msg", "全局公共异常!::" + e.getMessage());

        return XmlMapUtils.formatXml(XmlMapUtils.map2xml(errMap, "response"));
    }

    public String expHandler_bsoft(Exception e, String Sender, String MsgType, String MsgVersion) throws Exception {
        Map<String, Object> BSXml = new HashMap<>();
        Map<String, Object> MsgHeader = new HashMap<>();
        MsgHeader.put("Status", "false");
        MsgHeader.put("Sender", Sender);
        MsgHeader.put("ErrCode", "-1");
        MsgHeader.put("Detail", MsgVersion);
        if (e.getMessage() != null){
            MsgHeader.put("Detail", e.getMessage());
        }
        BSXml.put("MsgHeader", MsgHeader);
        return XmlMapUtils.formatXml(XmlMapUtils.map2xml(BSXml, "BSXml"));
    }

    /************金堂县第一人民医院*****************************四川智康科技有限责任公司**********************医疗数据需求规范CCIS系统接口*********************************/

    /*
     * 公共方法接口
     */
    @Override
    public String process(String req) throws Exception {
        logger.info("公共方法接口:入参:" + req);
        String res = "";
        try {
            Map<String, Object> reqMap = XmlMapUtils.xml2map(req, false);
            String method = reqMap.get("method").toString();

            switch (method) {
                case "CCIS-GetPatientIdByWard":
                    logger.info("进入:4.2.1获取指定病区的患者ID列表[CCIS-GetPatientIdByWard]");
                    res = ccisService.getPatientIdByWard(reqMap);
                    break;
                case "CCIS-GetPatientInfoByPatientId":
                    logger.info("进入:4.2.2指定ID的住院患者基本信息[CCIS-GetPatientInfoByPatientId]");
                    res = ccisService.getPatientInfoByPatientId(reqMap);
                    break;
                case "CCIS-GetPatientFlowInfoByPatientId":
                    logger.info("进入:4.2.3指定ID的住院患者流向信息[CCIS-GetPatientFlowInfoByPatientId]");
                    res = ccisService.getPatientFlowInfoByPatientId(reqMap);
                    break;
                case "CCIS-GetAdviceByPatientId":
                    logger.info("进入:4.3.1指定ID的住院患者ICU医嘱[CCIS-GetAdviceByPatientId]");
                    res = ccisService.getAdviceByPatientId(reqMap);
                    break;
                case "CCIS-ExecuteRecord":
                    logger.info("进入:4.3.2执行记录[CCIS-ExecuteRecord]");
                    res = ccisService.getExecuteRecord(reqMap);
                    break;
                case "CCIS-GetLabReportByPatientId":
                    logger.info("进入:4.4.1指定ID的检验报告[CCIS-GetLabReportByPatientId]");
                    res = ccisService.getLabReportByPatientId(reqMap);
                    break;
                case "CCIS-GetSpecialReportByPatientId":
                    logger.info("进入:4.4.2指定ID的检查报告[CCIS-GetSpecialReportByPatientId]");
                    res = ccisService.getSpecialReportByPatientId(reqMap);
                    break;
                case "CCIS-GetMedicalRecord":
                    logger.info("进入:根据住院号获取病历信息[CCIS-GetMedicalRecord]");
                    res = ccisService.get_Bl_MedicalRecord(reqMap);
                    break;
                case "CCIS-SaveFeverRecords":
                    logger.info("进入:保存体温单[CCIS-SaveFeverRecords]");
                    res = ccisService.saveFeverRecords(reqMap);
                    break;
                default:
                    logger.info("入参Method[" + method + "]错误!");
                    throw new CommonException("入参Method[" + method + "]错误!");
            }
        } catch (Exception e) {
            res = expHandler(e).toString();
        }
//        logger.info("公共方法接口:出参:" + (res.length() > 1000 ? res.substring(0, 200) : res));
        logger.info("公共方法接口:出参:" + (res.length() > 1000 ? res.substring(0, 200) : res));
        return res;
    }

    /************金堂县第一人民医院*****************************四川智康科技有限责任公司**********************医疗数据需求规范CCIS系统接口*********************************/

    /*********WHIS******住院医嘱计费(07)*********************************/
    /*
     * 门诊 取消/执行 检查状态更新
     */
    @Override
    public String InspectionStatusUpdate(String req) throws Exception {
        logger.info("门诊检查状态更新:入参：" + req);
        String res = "";
        try {
            Map<String, Object> reqs = (Map<String, Object>)XmlMapUtils.xml2map(req, false);

            res = hosOrderBillService.InspectionStatusUpdate((Map<String, Object>) reqs.get("MsgBody"));
        } catch (Exception e) {
            res = expHandler_bsoft(e, "HIS", "ODS_03070009_ERR", "3.3").toString();
        }
        logger.info("门诊检查状态更新:出参：" + res);
        return res;
    }


    /*
     * SVR_ODS_2204-检查计费确认通知
     */
    @Override
    public String ExmRequestPayBill(String req) throws Exception {

        logger.info("SVR_ODS_2204-检查计费确认通知:入参：" + req);
        String res = "";
        try {
            res = hosOrderBillService.ExmRequestPayBill((Map<String, Object>) XmlMapUtils.xml2map(req, false).get("ExmRequest"));
        } catch (Exception e) {
            res = expHandler_bsoft(e, "HIS", "ODS_2204", "3.0").toString();
        }
        logger.info("SVR_ODS_2204-检查计费确认通知:出参：" + res);
        return res;
    }

    /*
     * SVR_ODS_2205-取消检查计费通知
     */
    @Override
    public String ExmRequestReturnBill(String req) throws Exception {
        logger.info("SVR_ODS_2205-取消检查计费通知:入参：" + req);
        String res = "";
        try {
            res = hosOrderBillService.ExmRequestReturnBill((Map<String, Object>) XmlMapUtils.xml2map(req, false).get("ExmRequest"));
        } catch (Exception e) {
            res = expHandler_bsoft(e, "HIS", "ODS_2205", "3.0").toString();
        }
        logger.info("SVR_ODS_2205-取消检查计费通知:出参：" + res);
        return res;
    }


    /*
     * 非检查医嘱 计费(住院 无申请点ID 如:药品,治疗等)
     */
    @Override
    public String noExamineOrderBill(String req) throws Exception {

        logger.error("住院:非检查医嘱(无申请点ID)计费:入参：" + req);
        String res = "";
        try {
            res = hosOrderBillService.noExamineOrderBill((Map<String, Object>) XmlMapUtils.xml2map(req, false).get("MsgBody"));
        } catch (Exception e) {
            res = expHandler_bsoft(e, "HIS", "ODS_03070009_ERR", "3.3").toString();
        }
        logger.error("住院:非检查医嘱(无申请点ID)计费:出参：" + res);


        return res;
    }

    /*
     * 非检查医嘱 取消计费(住院 无申请点ID 如:药品,治疗等)
     */
    @Override
    public String noExamineOrderCancelBill(String req) throws Exception {
        logger.error("住院:非检查医嘱取消计费:入参：" + req);
        String res = "";
        try {
            res = hosOrderBillService.noExamineOrderCancelBill((Map<String, Object>) XmlMapUtils.xml2map(req, false).get("MsgBody"));
        } catch (Exception e) {
            res = expHandler_bsoft(e, "HIS", "ODS_03070009_ERR", "3.3").toString();
        }
        logger.error("住院:非检查医嘱取消计费:出参：" + res);
        return res;
    }
    /*********WHIS******住院医嘱计费(07)*********************************/

}

